﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Utils
{
    /// <summary>
    /// Web工具类
    /// Authot By lijunlin
    /// </summary>
    public class WebUtil
    {
        
        /// <summary>
        /// 获取当前路径
        /// </summary>
        /// <param name="fullPath">当前资源的全路径</param>
        /// <returns>当前路径字符串</returns>
        public static string GetCurrentPath(object fullPath)
        {
            string path = fullPath.ToString();
            int index = path.IndexOf("Page/") + 5;
            path = path.Substring(0, index);
            return path;
        }

        public static string GetImagesCurrentPath(object fullPath) 
        {
            string path = fullPath.ToString();
            int index = path.IndexOf("Page/");
            path = path.Substring(0, index);
            return path;
        }

        /// <summary>
        /// 显示提示信息并且跳转到指定URL页面
        /// </summary>
        /// <param name="message">提示信息</param>
        /// <param name="fullPath">当前资源的全路径</param>
        /// <param name="redirectURL">重定向资源路径</param>
        /// <returns>js脚本字符串</returns>
        public static string GetTipsAndRedirect(string message, object fullPath, string redirectURL)
        {
            StringBuilder js = new StringBuilder();

            js.Append("<script>alert('").Append(message).Append("');");
            if (redirectURL != null)
            {
                js.Append("window.location.href='");
                if (fullPath != null)
                {
                    js.Append(GetCurrentPath(fullPath));
                }
                js.Append(redirectURL).Append("';");
            }
            js.Append("</script>");

            return js.ToString();
        }


        /// <summary>
        /// 显示提示信息并且跳转到指定URL页面
        /// </summary>
        /// <param name="message">提示信息</param>
        /// <param name="redirectURL">重定向资源路径</param>
        /// <returns>js脚本字符串</returns>
        public static string GetTipsAndRedirect(string message, string redirectURL)
        {
            return GetTipsAndRedirect(message, null, redirectURL);
        }


        /// <summary>
        /// 显示提示信息
        /// </summary>
        /// <param name="message">提示信息</param>
        /// <returns>js脚本字符串</returns>
        public static string GetTips(string message)
        {
            return GetTipsAndRedirect(message, null, null);
        }


        /// <summary>
        /// 返回到上一个访问页面
        /// </summary>
        /// <returns>js脚本：history.back()</returns>
        public static string Back()
        {
            StringBuilder js = new StringBuilder();

            js.Append("<script>");
            js.Append("window.history.back();");
            js.Append("</script>");

            return js.ToString();
        }


        /// <summary>
        /// 访问下一个访问页面
        /// </summary>
        /// <returns>js脚本：history.forward()</returns>
        public static string Forward()
        {
            StringBuilder js = new StringBuilder();

            js.Append("<script>");
            js.Append("window.history.forward();");
            js.Append("</script>");

            return js.ToString();
        }


        /// <summary>
        /// 获取全站唯一标识符：Session["userId"]
        /// </summary>
        /// <param name="identifier">唯一标识符</param>
        /// <returns>格式正确的唯一标识符字符串</returns>
        public static string GetIdentifier(object identifier)
        {
            if (identifier == null) identifier = "";

            return identifier.ToString();
        }


    }
}
